package UI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 添加员工界面
 * 
 * Version: 1.0
 * Author: 瓶子
 */

public class AddEmployeesUI extends JFrame {

    private JTextField idField;
    private JTextField nameField;
    private JComboBox<String> departmentCombo;
    private JComboBox<String> positionCombo;
    private JTextField salaryField;
    private JTextField dateField;

    public AddEmployeesUI() {
        // 设置窗口属性
        setTitle("添加新员工");
        setSize(500, 400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);

        // 创建主面板
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout(10, 10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        mainPanel.setBackground(new Color(245, 248, 250));

        // 添加标题
        JLabel titleLabel = new JLabel("添加新员工信息", SwingConstants.CENTER);
        titleLabel.setFont(new Font("楷体", Font.BOLD, 24));
        titleLabel.setForeground(new Color(25, 118, 210));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 15, 0));
        mainPanel.add(titleLabel, BorderLayout.NORTH);

        // 创建表单面板
        JPanel formPanel = new JPanel();
        formPanel.setLayout(new GridLayout(6, 2, 10, 15));
        formPanel.setBackground(Color.WHITE);
        formPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(200, 210, 220)),
                BorderFactory.createEmptyBorder(20, 20, 20, 20)
                                                              ));

        // 添加表单字段
        addFormField(formPanel, "员工ID:", idField = createTextField());
        addFormField(formPanel, "姓名:", nameField = createTextField());

        // 部门下拉框
        String[] departments = {"技术部", "市场部", "财务部", "人力资源", "销售部"};
        departmentCombo = createComboBox(departments);
        addFormField(formPanel, "部门:", departmentCombo);

        // 职位下拉框
        String[] positions = {"工程师", "经理", "主管", "分析师", "助理"};
        positionCombo = createComboBox(positions);
        addFormField(formPanel, "职位:", positionCombo);

        addFormField(formPanel, "薪资:", salaryField = createTextField());

        // 入职日期字段
        dateField = createTextField();
        dateField.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        dateField.setEditable(false); // 入职日期不可编辑
        addFormField(formPanel, "入职日期:", dateField);

        mainPanel.add(formPanel, BorderLayout.CENTER);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 10));
        buttonPanel.setBackground(new Color(245, 248, 250));
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));

        // 添加按钮
        JButton addButton = createButton("添加员工", new Color(46, 204, 113));
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addEmployee();
            }
        });

        // 取消按钮
        JButton cancelButton = createButton("取消", new Color(120, 130, 140));
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        buttonPanel.add(addButton);
        buttonPanel.add(cancelButton);

        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        add(mainPanel);

        // 设置窗口可见后立即获得焦点
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                idField.requestFocusInWindow();
            }
        });
        setVisible(true);
    }

    private void addFormField(JPanel panel, String label, JComponent field) {
        JLabel jLabel = new JLabel(label);
        jLabel.setFont(new Font("楷体", Font.BOLD, 16));
        jLabel.setForeground(new Color(70, 70, 70));
        panel.add(jLabel);
        panel.add(field);
    }

    private JTextField createTextField() {
        JTextField textField = new JTextField();
        textField.setFont(new Font("楷体", Font.PLAIN, 14));
        textField.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(180, 190, 200)),
                BorderFactory.createEmptyBorder(8, 10, 8, 10)
                                                              ));
        textField.setEnabled(true); // 确保文本框可编辑
        textField.setEditable(true); // 确保文本框可输入
        return textField;
    }

    private JComboBox<String> createComboBox(String[] items) {
        JComboBox<String> comboBox = new JComboBox<>(items);
        comboBox.setFont(new Font("楷体", Font.PLAIN, 14));
        comboBox.setBackground(Color.WHITE);
        comboBox.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(180, 190, 200)),
                BorderFactory.createEmptyBorder(5, 10, 5, 10)
                                                             ));
        comboBox.setEnabled(true); // 确保下拉框可用
        comboBox.setEditable(false); // 下拉框不可编辑，但可以选择
        return comboBox;
    }

    private JButton createButton(String text, Color color) {
        JButton button = new JButton(text);
        button.setFont(new Font("楷体", Font.BOLD, 16));
        button.setBackground(color);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(color.darker()),
                BorderFactory.createEmptyBorder(8, 25, 8, 25)
                                                           ));

        // 添加悬停效果
        button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button.setBackground(color.darker());
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                button.setBackground(color);
            }
        });

        return button;
    }

    private void addEmployee() {
        // 获取表单数据
        String id = idField.getText().trim();
        String name = nameField.getText().trim();
        String department = (String) departmentCombo.getSelectedItem();
        String position = (String) positionCombo.getSelectedItem();
        String salary = salaryField.getText().trim();
        String date = dateField.getText().trim();

        // 验证输入
        if (id.isEmpty() || name.isEmpty() || salary.isEmpty()) {
            JOptionPane.showMessageDialog(this,
                                          "请填写所有必填字段！",
                                          "输入错误",
                                          JOptionPane.ERROR_MESSAGE);
            return;
        }

        // 显示成功信息
        String message = String.format(
                "<html><div style='font-family:楷体;font-size:14px;text-align:center;'>" +
                        "<b>员工信息添加成功！</b><br><br>" +
                        "ID: %s<br>姓名: %s<br>部门: %s<br>职位: %s<br>薪资: %s<br>入职日期: %s" +
                        "</div></html>",
                id, name, department, position, salary, date
                                      );

        JLabel messageLabel = new JLabel(message);
        messageLabel.setHorizontalAlignment(SwingConstants.CENTER);

        JOptionPane.showMessageDialog(
                this,
                messageLabel,
                "添加成功",
                JOptionPane.INFORMATION_MESSAGE
                                     );

        // 重置表单
        idField.setText("");
        nameField.setText("");
        departmentCombo.setSelectedIndex(0);
        positionCombo.setSelectedIndex(0);
        salaryField.setText("");
        idField.requestFocus(); // 重置后聚焦到ID输入框
    }

    public static void main(String[] args) {
        new AddEmployeesUI();
    }
}